package trafficgeneratorcr.algo;

public class LinearInterpolationAlgorithm extends InterpolationAlgorithm {

    public LinearInterpolationAlgorithm(double[] knownPoints, int wantedPointCount) {
        super(knownPoints, wantedPointCount);
    }

    @Override
    public void execute() {
        int expand = (wantedPointCount - knownPoints.length) / knownPoints.length;

        for (int knownPointIndex = 0; knownPointIndex < knownPoints.length - 1; knownPointIndex++) {
            double valueInf = knownPoints[knownPointIndex];
            double valueSup = knownPoints[knownPointIndex + 1];
            for (int interpolatedPointSubIndex = 0; interpolatedPointSubIndex < expand + 1; interpolatedPointSubIndex++) {
                int interpolatedPointIndex = knownPointIndex * (expand + 1) + interpolatedPointSubIndex;
                double interpolatedValue = valueInf + (valueSup - valueInf) * (interpolatedPointIndex - knownPointIndex * (expand + 1)) / (expand + 1);

                interpolatedPoints[interpolatedPointIndex] = interpolatedValue;
            }
        }

        // workaround for the last sample
        interpolatedPoints[interpolatedPoints.length - 1] = knownPoints[knownPoints.length - 1];
    }

}
